'use strict';

/**
 * @ngdoc function
 * @name jgappApp.controller:ChatRoomdetailCtrl
 * @description
 * # ChatRoomdetailCtrl
 * Controller of the jgappApp
 */
angular.module('jgappApp')
    .controller('ChatRoomdetailCtrl', function($scope, $routeParams, ChatStatus, ChatUser, RongYService, $location, Util) {
        var discussionId = $routeParams.id
        var vm = this;
        vm.logo = ChatUser.DiscussionImg
        var param = {}
        param.discussionId = discussionId;
        this.quitDiscusson = function() {
            var members = vm.discussionDetail.memberIdList.filter(function(item) {
                return item !== vm.discussionDetail.creatorId
            })
            param.members = members
            if (Util.isCurId(vm.discussionDetail.creatorId)) {
                RongYService.removeAllMemberFromDiscussion(param).then(function(data) {
                    RongYService.quitDiscussion({ discussionId: discussionId }).then(function(data) {
                        // 最后清除相对应的会话
                        removeConversation("已经解散讨论组")
                    })
                })
            } else {
                // 退出群组
                RongYService.quitDiscussion({ discussionId: discussionId }).then(function(data) {
                    // 最后清除相对应的会话
                    removeConversation("已经退出讨论组")
                })
            }

        }
        var removeConversation = function(msg) {
            var conversation = {
                conversationType: "DISCUSSION",
                targetId: discussionId
            }
            RongYService.removeConversation(conversation).then(function(data) {
                Util.Toast(msg, {
                    callback: function() {
                        $location.path("chat/index").replace();
                    }
                })
            })
        }
        this.discussonUsers = function(lists) {
            ChatStatus.setUsers(lists);
            $location.path("chat/users/" + discussionId);
        }
        this.switchNotice = function() {
            var notificationStatus = "";
            notificationStatus = vm.checked ? "DO_NOT_DISTURB" : "NOTIFY";
            RongYService.setConversationNotificationStatus(discussionId, ChatStatus.getChatType(), notificationStatus).then(function(data) {
                vm.checked = !!data.code
            })
        };
        var getDiscussionDetail = function() {
            RongYService.getDiscussion(param).then(function(data) {
                if (data) {
                    if (data == 21406) {
                        removeConversation("该讨论组已经解散")
                    } else {

                        vm.discussionDetail = data;
                    }
                }
            })
        }
        var getNotificationStatus = function() {
            RongYService.getConversationNotificationStatus(discussionId, ChatStatus.getChatType()).then(function(data) {
                vm.checked = !!data.code
            })
        }
        var init = function() {
            getDiscussionDetail()
            getNotificationStatus();
        }
        init();
    });
